package com.moji.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjCombo;
import com.moji.entity.dto.QueryAboutUsDto;
import com.moji.entity.dto.QueryComboDto;
import com.moji.mapper.MjComboMapper;
import com.moji.service.MjComboService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.util.IdGenerator;
import com.moji.util.MoJiCommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 套餐表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2024-07-20
 */
@Service
public class MjComboServiceImpl extends ServiceImpl<MjComboMapper, MjCombo> implements MjComboService {

    @Resource
    private IdGenerator idGenerator;

    @Override
    public MyPage<MjCombo> queryByPage(QueryComboDto dto) {
        LambdaQueryWrapper<MjCombo> wrapper = new LambdaQueryWrapper<>();
        //标题
        wrapper.like(ObjectUtil.isNotEmpty(dto.getTitle()),MjCombo::getTitle
                ,dto.getTitle());
        //创建时间倒叙
        wrapper.orderByDesc(MjCombo::getCreateTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<MjCombo> MjComboList = list(wrapper);
        PageInfo<MjCombo> info = new PageInfo<>(MjComboList);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.removeById(id);
    }

    @Override
    public boolean add(MjCombo MjCombo) {
        //编码自动生成
        String code = idGenerator.idGenerator("TCGL");
        MjCombo.setCode(code);
        MjCombo.setCreateId(MoJiCommonUtils.getUserCode());
        MjCombo.setUpdateId(MoJiCommonUtils.getUserCode());
        MjCombo.setCreateTime(new Date());
        MjCombo.setUpdateTime(new Date());
        return this.save(MjCombo);
    }

    @Override
    public boolean edit(MjCombo MjCombo) {
        MjCombo MjComboOld=this.getById(MjCombo.getId());
        if(MjComboOld ==null){
            throw new RuntimeException("数据不存在,请核查!");
        }
        BeanUtils.copyProperties(MjCombo,MjComboOld);
        MjCombo.setUpdateId(MoJiCommonUtils.getUserCode());
        MjCombo.setUpdateTime(new Date());
        return this.updateById(MjCombo);
    }

    @Override
    public MjCombo detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("主键ID不能为空,请核查!");
        }
        return this.getById(id);
    }
}
